home *** CD-ROM | disk | FTP | other *** search
/ Steal This CD / steal_this_cd.iso / Chapter 07 - Where the Hackers Are / virc200.exe / {app} / Scripts / tictactoe.vsc < prev    next >
Text File  |  2003-05-16  |  7KB  |  251 lines

  1. // Saved by ViRC 2.0 at 6/15/2001 4:50:01 PM
  2.  
  3. // Whiteboard tic-tac-toe game for Visual IRC 2.0pre8 and above
  4. // Jesse McGrew
  5.  
  6. if ($build < 200) || (($prebuild != 0) && ($prebuild < 8))
  7.   MessageBox This script requires ViRC 2.0pre8 or later.
  8.   halt
  9. endif
  10.  
  11. -@ $TTT.*
  12.  
  13. // Toolbars
  14.  
  15. Toolbar TB_TICTACTOE "Tic-tac-toe" for whiteboards docked top position last
  16.   T_PLAY   0 TTT | Play tic-tac-toe
  17.   T_REDRAW 0 Redraw | Redraw tic-tac-toe board
  18. EndToolbar
  19.  
  20. ToolbarItem T_PLAY on TB_TICTACTOE
  21.   //@l $id = $getwindowid($C)
  22.   @l $id = $strtokr(! $C)
  23.   if [${TTT.$id.State}] != []
  24.     @l $ret = $messagedlg(33 This will end the game in progress.)
  25.     halt if $ret != 1
  26.   endif
  27.   @ $TTT.$id.State = 0
  28.   @ $TTT.$id.Grid = ---------
  29.   TTT_RedrawBoard $id
  30. EndToolbarItem
  31.  
  32. ToolbarItem T_REDRAW on TB_TICTACTOE
  33.   @l $id = $strtokr(! $C)
  34.   TTT_RedrawBoard $id
  35. EndToolbarItem
  36.  
  37.  
  38. Event <OnMyWhiteboardAction_TTT> "% DR *"
  39.   // local user is X (moves in state 0)
  40.   
  41.   @l $id = $strtokr(! $0)
  42.   @l $grid = ${TTT.$id.Grid}
  43.   if ([$grid] != []) && (${TTT.$id.State} == 0)
  44.     // extract x and y from command
  45.     @l $cmd = $listsplit(, $2-)
  46.     @l $x = $listindex(4 $cmd)
  47.     @l $y = $listindex(5 $cmd)
  48.     // see if it's in a space
  49.     for (@l $i = 1; $i <= 9; $i++)
  50.       @l $x1 = $_ttt($i x1)
  51.       @l $y1 = $_ttt($i y1)
  52.       @l $x2 = $_ttt($i x2)
  53.       @l $y2 = $_ttt($i y2)
  54.       if ($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)
  55.         // got a space.. check the value
  56.         @l $value = $substr($grid $i 1)
  57.         if [$value] == [-]
  58.           // the space is empty
  59.           @ $TTT.$id.Grid = $substr($grid 1 $($i - 1))X$substr($grid $($i + 1) 9)
  60.           @ $TTT.$id.State = 1
  61.           TTT_RedrawBoard $id
  62.           TTT_CheckWinner $id
  63.         endif
  64.         halt
  65.       endif
  66.     endfor
  67.   endif
  68. EndEvent
  69.  
  70. Event <OnWhiteboardAction_TTT> "% DR *"
  71.   // remote user is O (moves in state 1)
  72.   
  73.   @l $id = $strtokr(! $0)
  74.   @l $grid = ${TTT.$id.Grid}
  75.   if ([$grid] != []) && (${TTT.$id.State} == 1)
  76.     // extract x and y from command
  77.     @l $cmd = $listsplit(, $2-)
  78.     @l $x = $listindex(4 $cmd)
  79.     @l $y = $listindex(5 $cmd)
  80.     // see if it's in a space
  81.     for (@l $i = 1; $i <= 9; $i++)
  82.       @l $x1 = $_ttt($i x1)
  83.       @l $y1 = $_ttt($i y1)
  84.       @l $x2 = $_ttt($i x2)
  85.       @l $y2 = $_ttt($i y2)
  86.       if ($x >= $x1) && ($x <= $x2) && ($y >= $y1) && ($y <= $y2)
  87.         // got a space.. check the value
  88.         @l $value = $substr($grid $i 1)
  89.         if [$value] == [-]
  90.           // the space is empty
  91.           @ $TTT.$id.Grid = $substr($grid 1 $($i - 1))O$substr($grid $($i + 1) 9)
  92.           @ $TTT.$id.State = 0
  93.           TTT_RedrawBoard $id
  94.           TTT_CheckWinner $id
  95.         endif
  96.         halt
  97.       endif
  98.     endfor
  99.   endif
  100. EndEvent
  101.  
  102.  
  103. Alias TTT_REDRAWBOARD
  104.   halt if [${TTT.$1.State}] == []
  105.   
  106.   @l $win = =!$1
  107.   // clear canvas
  108.   Whiteboard $win cls
  109.   // draw title
  110.   switch $TTT.$1.State
  111.     case 0:
  112.       TTT_Title $1 X's turn
  113.     case 1:
  114.       TTT_Title $1 O's turn
  115.     case else
  116.       TTT_Title $1 ViRC Tic-Tac-Toe
  117.   endswitch
  118.   // draw grid contents
  119.   @l $grid = ${TTT.$1.Grid}
  120.   halt if [$grid] == []
  121.   if [$grid] != []
  122.     for (@l $i = 1; $i <= 9; $i++)
  123.       @l $x1 = $_ttt($i x1)
  124.       @l $y1 = $_ttt($i y1)
  125.       @l $x2 = $_ttt($i x2)
  126.       @l $y2 = $_ttt($i y2)
  127.       switch $substr($grid $i 1)
  128.         case X:
  129.           // fill blue
  130.           Whiteboard $win dr 5 2 clBlue clBlue $x1,$y1 $x2,$y2
  131.           // NW-SE
  132.           Whiteboard $win dr 1 2 clBlack clBlack $($x1 + 5),$($y1 + 5) $($x2 - 5),$($y2 - 5)
  133.           // NE-SW
  134.           Whiteboard $win dr 1 2 clBlack clBlack $($x2 - 5),$($y1 + 5) $($x1 + 5),$($y2 - 5)
  135.         case O:
  136.           // fill red
  137.           Whiteboard $win dr 5 2 clRed clRed $x1,$y1 $x2,$y2
  138.           // circle
  139.           Whiteboard $win dr 4 2 clBlack clBlack $($x1 + 5),$($y1 + 5) $($x2 - 5),$($y2 - 5)
  140.       endswitch
  141.     endfor
  142.   endif
  143.   
  144.   // draw grid lines
  145.   // vertical left: (2.x1,2.y1)-(7.x2,7.y2)
  146.   Whiteboard $win dr 1 2 clBlack clBlack $_ttt(2 x1),$_ttt(2 y1) $_ttt(7 x2),$_ttt(7 y2)
  147.   // vertical right: (3.x1,3.y1)-(8.x2,8.y2)
  148.   Whiteboard $win dr 1 2 clBlack clBlack $_ttt(3 x1),$_ttt(3 y1) $_ttt(8 x2),$_ttt(8 y2)
  149.   // horizontal top: (4.x1,4.y1)-(6.x2,6.y1)
  150.   Whiteboard $win dr 1 2 clBlack clBlack $_ttt(4 x1),$_ttt(4 y1) $_ttt(6 x2),$_ttt(6 y1)
  151.   // horizontal bottom: (7.x1,7.y1)-(9.x2,9.y1)
  152.   Whiteboard $win dr 1 2 clBlack clBlack $_ttt(7 x1),$_ttt(7 y1) $_ttt(9 x2),$_ttt(9 y1)
  153. EndAlias
  154.  
  155. Alias _TTT
  156.   @l $GRID_BASEX = 10
  157.   @l $GRID_BASEY = 70
  158.   @l $GRID_SIZE = 50
  159.   
  160.   @l $row = $(($1 - 1) / 3)
  161.   @l $col = $(($1 - 1) % 3)
  162.   @l $x1 = $($col * $GRID_SIZE + $GRID_BASEX)
  163.   @l $y1 = $($row * $GRID_SIZE + $GRID_BASEY)
  164.   
  165.   switch $2
  166.     case x1:
  167.       @ $fresult = $x1
  168.     case y1:
  169.       @ $fresult = $y1
  170.     case x2:
  171.       @ $fresult = $($x1 + $GRID_SIZE)
  172.     case y2:
  173.       @ $fresult = $($y1 + $GRID_SIZE)
  174.     case xc:
  175.       @ $fresult = $($x1 + $GRID_SIZE / 2)
  176.     case yc:
  177.       @ $fresult = $($y1 + $GRID_SIZE / 2)
  178.   endswitch
  179. EndAlias
  180.  
  181. Alias TTT_CHECKWINNER
  182.   @l $id = $1
  183.   // horizontal
  184.   foreach ($a,$b,$c; 1 2 3 4 5 6 7 8 9)
  185.     if $ttt_line($id $a $b $c)
  186.       TTT_Winner $id $a $b $c
  187.       halt
  188.     endif
  189.   endforeach
  190.   
  191.   // vertical
  192.   foreach ($a,$b,$c; 1 4 7 2 5 8 3 6 9)
  193.     if $ttt_line($id $a $b $c)
  194.       TTT_Winner $id $a $b $c
  195.       halt
  196.     endif
  197.   endforeach
  198.   
  199.   // diagonal
  200.   if $ttt_line($id 1 5 9)
  201.     TTT_Winner $id 1 5 9
  202.     halt
  203.   endif
  204.   if $ttt_line($id 3 5 7)
  205.     TTT_Winner $id 3 5 7
  206.     halt
  207.   endif
  208.   
  209.   // cat's game?
  210.   if $strpos(- $TTT.$id.Grid) == 0
  211.     TTT_Title $id Cat's game :(
  212.     -@ $TTT.$id.*
  213.     halt
  214.   endif
  215. EndAlias
  216.  
  217. Alias TTT_WINNER
  218.   @l $id = $1
  219.   // starting and ending space
  220.   @l $s1 = $2
  221.   @l $s2 = $4
  222.   // draw a line
  223.   Whiteboard =!$id dr 1 24 clYellow clYellow $_ttt($s1 xc),$_ttt($s1 yc) $_ttt($s2 xc),$_ttt($s2 yc)
  224.   // draw winner's title
  225.   TTT_Title $id $substr($TTT.$id.Grid $s1 1) wins!!
  226.   // end game
  227.   -@ $TTT.$id.*
  228. EndAlias
  229.  
  230. Alias TTT_TITLE
  231.   // draw box for title
  232.   Whiteboard =!$1 dr 5 2 clBlack clYellow 10,10 190,50
  233.   // draw title
  234.   Whiteboard =!$1 txtex 20,20 Arial,14 2 clBlack clYellow $2-
  235. EndAlias
  236.  
  237. Alias TTT_LINE
  238.   @l $grid = ${TTT.$1.Grid}
  239.   if [$grid] == []
  240.     @ $fresult = 0
  241.     halt
  242.   endif
  243.   
  244.   // get space values
  245.   @l $v1 = $substr($grid $2 1)
  246.   @l $v2 = $substr($grid $3 1)
  247.   @l $v3 = $substr($grid $4 1)
  248.   // return 1 if s1 isn't empty, s1 == s2, and s1 == s3
  249.   @ $fresult = $(([$v1] != [-]) && ([$v1] == [$v2]) && ([$v1] == [$v3]))
  250. EndAlias
  251.